# 对于电影而言，普通用户只有读权限，后台管理员有写权限

from flask_restful import Resource, reqparse, fields, marshal_with, abort, marshal
from werkzeug.datastructures import FileStorage

from App.apis.admin.utils import login_required
from App.apis.api_constant import HTTP_OK, HTTP_CREATE_OK
from App.apis.common.utils import filename_transform
from App.models.common.movie_model import Movie
from App.settings import UPLOADS_DIR, FILE_PATH_PREFIX

parse = reqparse.RequestParser()

parse.add_argument("showname", required=True, help="must supply showname")
parse.add_argument("shownameen", required=True, help="must supply shownameen")
parse.add_argument("director", required=True, help="must supply director")
parse.add_argument("leadingRole", required=True, help="must supply leadingRole")
parse.add_argument("movie_type", required=True , help="must supply type")
parse.add_argument("country", required=True, help="must supply country")
parse.add_argument("language", required=True, help="must supply language")
parse.add_argument("duration", required=True, help="must supply duration")
parse.add_argument("screeningmodel", required=True, help="must supply screeningmodel")
parse.add_argument("openday", required=True, help="must supply openday")
parse.add_argument("backgroundpicture", type=FileStorage, required=True, help="must supply backgroundpicture", location=['files'])

movie_fields = {
    "showname": fields.String,
    "shownameen": fields.String,
    "director": fields.String,
    "leadingRole": fields.String,
    "type": fields.String,
    "country": fields.String,
    "language": fields.String,
    "duration": fields.Integer,
    "screeningmodel": fields.String,
    "openday": fields.DateTime,
    "backgroundpicture": fields.String,
}

multi_movies_fields = {
    "status": fields.Integer,
    "msg": fields.String,
    "data": fields.List(fields.Nested(movie_fields))
}

# 电影查询无论是谁都能查，创建只能是管理端才能创建，要添加login的验证
class MoviesResource(Resource):
    @marshal_with(multi_movies_fields)
    def get(self):
        movies = Movie.query.all()
        data = {
            "msg": "ok",
            "status": HTTP_OK,
            "data": movies
        }

        return data


    # 注意这个login_required来源有两个，一个是对用户的token验证，一个是对管理员的token验证。这里注意不要导错包
    @login_required
    def post(self):
        args = parse.parse_args()

        showname = args.get("showname")
        shownameen = args.get("shownameen")
        director = args.get("director")
        leadingRole = args.get("leadingRole")
        movie_type = args.get("movie_type")
        country = args.get("country")
        language = args.get("language")
        duration = args.get("duration")
        screeningmodel = args.get("screeningmodel")
        openday = args.get("openday")
        backgroundpicture = args.get("backgroundpicture")   # 直接在postman上选择的，真正要拿估计也要原始文件的路径

        # backgroundpicture = request.files.get("backgroundpicture")
        # print(type(backgroundpicture))  #'''<class 'werkzeug.datastructures.FileStorage'>'''
        # print(backgroundpicture)        #'''<FileStorage: 'bd_logo1.png' ('image/png')>'''
        movie = Movie()
        movie.showname = showname
        movie.shownameen = shownameen
        movie.director = director
        movie.leadingRole = leadingRole
        movie.type = movie_type
        movie.country = country
        movie.language = language
        movie.duration = duration
        movie.screeningmodel = screeningmodel
        movie.openday = openday

        # 文件上传分两步：第一步：想要存到哪里就把哪里的路径加上，配合想要给文件取的名字构成整体；第二步：把文件路径包括文件名一起save到想要save到的地方，想存表就存
        new_filename = filename_transform(backgroundpicture.filename)
        filepath = UPLOADS_DIR + new_filename
        backgroundpicture.save(filepath)
        # movie.backgroundpicture = FILE_PATH_PREFIX + backgroundpicture.filename    # 这里其实还不够严谨，文件名可能会有重复的情况，应该要拼接一串唯一标识
        movie.backgroundpicture = FILE_PATH_PREFIX +new_filename

        if not movie.save():
            abort(400, msg="can't create movie")

        data = {
            "msg": "create success",
            "status": HTTP_CREATE_OK,
            "data": marshal(movie, movie_fields)
        }

        return data

class MovieResource(Resource):
    def get(self, id):
        movie = Movie.query.filter(Movie.id == id).first()
        if not movie:
            abort(404, msg="movie is not exist")
        data = {
            "msg": "ok",
            "status": HTTP_OK,
            "data": marshal(movie, movie_fields)
        }
        return data

    @login_required
    def patch(self, id):
        return {'msg': "post ok"}
    @login_required
    def put(self, id):
        return {'msg': "post ok"}

    @login_required
    def delete(self, id):
        return {'msg': "post ok"}




